package cn.loioi.distribution.spring.db;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.transaction.TransactionStatus;

import cn.loioi.distribution.db.DbConnoction;

public class SpringDbConnoction extends DbConnoction {
	private TransactionStatus transactionStatus;

	public SpringDbConnoction(DataSource dataSource) throws SQLException {
		super(dataSource);
	}

	public SpringDbConnoction(DataSource dataSource, TransactionStatus transactionStatus) throws SQLException {
		super(dataSource);
		this.transactionStatus = transactionStatus;
	}

	/**
	 * 获取事务状态
	 * @return
	 */
	public TransactionStatus getTransactionStatus() {
		return transactionStatus;
	}

	/**
	 * 添加事务状态
	 * @param transactionStatus
	 */
	public void setTransactionStatus(TransactionStatus transactionStatus) {
		this.transactionStatus = transactionStatus;
	}

	/**
	 * 判断是不是回滚
	 * @return
	 */
	@Override
	public boolean isRollbackOnly() {
		boolean sp = super.isRollbackOnly();

		if (transactionStatus != null) {
			return sp && transactionStatus.isRollbackOnly();
		} else {
			return sp;
		}
	}

	/**
	 * 设置为回滚
	 */
	@Override
	public void setRollbackOnly() {
		super.setRollbackOnly();

		if (transactionStatus != null) {
			transactionStatus.setRollbackOnly();
		}
	}
}
